import zmq
import time
import threading
def publisher():
    context = zmq.Context()
    socket = context.socket(zmq.PUB)
    socket.bind("tcp://*:5555")
    while True:
        topic = "topic1"
        message = "Hello from publisher"
        socket.send_multipart([bytes(topic, 'utf-8'), bytes(message, 'utf-8')])
        time.sleep(1)

def subscriber(name):
    context = zmq.Context()
    socket = context.socket(zmq.SUB)
    socket.connect("tcp://localhost:5555")
    topicfilter = "topic1"
    socket.setsockopt_string(zmq.SUBSCRIBE, topicfilter)
    while True:
        [topic, message] = socket.recv_multipart()
        print(f"{name} received: {message}")



if __name__ == "__main__":
    pub_thread = threading.Thread(target=publisher)
    pub_thread.start()
    sub_thread1 = threading.Thread(target=subscriber, args=("Subscriber 1",))
    sub_thread1.start()
    sub_thread2 = threading.Thread(target=subscriber, args=("Subscriber 2",))
    sub_thread2.start()